package com.gj.daoImp;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import com.gj.bean.ContactsBean;
import com.gj.bean.UserBean;
import com.gj.dao.ContactDao;
import com.gj.util.SqlHelper;
import com.gj.util.WriteLog;

public class ContactDaoImp implements ContactDao{
	 SqlHelper sql;
	/* (non-Javadoc)
	 * @see com.gj.daoImp.ContactDao#addContact(com.gj.bean.ContactsBean)
	 */
	@Override
	public int addContact(ContactsBean contacts){
		String sqlstr="insert into contact (ContactName,ContactNum,CategoryId,mark,address,userId) values(?,?,?,?,?,?)";
		String[] param={contacts.getContactName(),contacts.getContectNum(),String.valueOf(contacts.getCategoryId()),contacts.getMark(),contacts.getAddress(),String.valueOf(contacts.getUserId())};
		int result=sql.ExecSql(sqlstr,param);
		System.out.println("1为："+contacts.getContactName());
		System.out.println("2为："+contacts.getContectNum());
		System.out.println("3为："+contacts.getCategoryId());
		System.out.println("4为："+contacts.getAddress());
		System.out.println("5为："+contacts.getMark());
		System.out.println("6为："+contacts.getUserId());
		return result;
		
		
	}
	/* (non-Javadoc)
	 * @see com.gj.daoImp.ContactDao#deleteContact(int)
	 */
	@Override
	public int deleteContact(int contactId){
		String sqlstr="delete from contact where contactId=?";
		int param=contactId;
		int result=sql.ExecSql(sqlstr,param);
		
		return result;
		
	}
	/* (non-Javadoc)
	 * @see com.gj.daoImp.ContactDao#UpdateContact(int)
	 */
	@Override
	public int UpdateContact(ContactsBean contacts,int contactId){
		String sqlstr="update contact set ContactName=?,ContactNum=?,CategoryId=?,mark=?,address=?,userId=? where ContactId=?";
		String param[]={contacts.getContactName(),contacts.getContectNum(),String.valueOf(contacts.getCategoryId()),contacts.getMark(),contacts.getAddress(),String.valueOf(contacts.getUserId()),String.valueOf(contactId)};
		int result=SqlHelper.ExecSql(sqlstr, param);
		System.out.println("lenth为："+contacts.getContactName());
		System.out.println("lenth为："+contacts.getContectNum());
		System.out.println("lenth为："+contacts.getCategoryId());
		System.out.println("lenth为："+contacts.getUserId());
		System.out.println("lenth为："+contacts.getMark());
		System.out.println("lenth为："+contactId);
		return result;
		
		
	}
	/* (non-Javadoc)
	 * @see com.gj.daoImp.ContactDao#getContactInfo()
	 */
	@Override
	public  ArrayList<ContactsBean> getContactInfo(){
		String sqlSt="select * from contact";
		ResultSet result=sql.getResultSet(sqlSt);
		ArrayList<ContactsBean> users=new ArrayList<ContactsBean>();
		try {
			while(result.next()){
				
				
				ContactsBean  contacts=new ContactsBean();
				contacts.setCategoryId(Integer.parseInt(result.getString("CategoryId")));
				contacts.setAddress(result.getString("address"));
				contacts.setContactName(result.getString("ContactName"));
				contacts.setContectNum(result.getString("ContactNum"));
				contacts.setMark(result.getString("mark"));
				contacts.setContactId(Integer.parseInt(result.getString("ContactId")));
				users.add(contacts);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			WriteLog.getLog4j().logger.debug(e.getMessage());
		}
		return users;
		
	}
	//查找联系人
		/* (non-Javadoc)
		 * @see com.gj.daoImp.ContactDao#findContacts(int)
		 */
		@Override
		public ArrayList<ContactsBean> findContacts(int userId) throws NumberFormatException, SQLException{
			String sqlSt="select * from contact where userId=?";
			ResultSet result=sql.getResultSet(sqlSt,userId);
			ArrayList<ContactsBean> users=new ArrayList<ContactsBean>();
			
				while(result.next()){
					
					
					ContactsBean  contacts=new ContactsBean();
					contacts.setCategoryId(Integer.parseInt(result.getString("CategoryId")));
					contacts.setAddress(result.getString("address"));
					contacts.setContactName(result.getString("ContactName"));
					contacts.setContectNum(result.getString("ContactNum"));
					contacts.setMark(result.getString("mark"));
					contacts.setContactId(Integer.parseInt(result.getString("ContactId")));
					users.add(contacts);
				}
			
			return users;
			
		}
		/**
		 * 通过分类查找
		 */
		@Override
		public ArrayList<ContactsBean> getContactInfoBygroup(int CategoryId) {
			// TODO Auto-generated method stub
			String sqlSt="select * from contact where CategoryId=?";
			int param=CategoryId;
			ResultSet result=sql.getResultSet(sqlSt,param);
			ArrayList<ContactsBean> users=new ArrayList<ContactsBean>();
			try {
				while(result.next()){
					
					
					ContactsBean  contacts=new ContactsBean();
					contacts.setCategoryId(Integer.parseInt(result.getString("CategoryId")));
					contacts.setAddress(result.getString("address"));
					contacts.setContactName(result.getString("ContactName"));
					contacts.setContectNum(result.getString("ContactNum"));
					contacts.setMark(result.getString("mark"));
					contacts.setContactId(Integer.parseInt(result.getString("ContactId")));
					users.add(contacts);
				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				WriteLog.getLog4j().logger.debug(e.getMessage());
			}
			return users;
		}
		/* (non-Javadoc)
		 * 通过id
		 */
		@Override
		public ArrayList<ContactsBean> getContactInfoId(int ContactId) {
			// TODO Auto-generated method stub
			String sqlSt="select * from contact where ContactId=?";
			ResultSet result=sql.getResultSet(sqlSt,ContactId);
			ArrayList<ContactsBean> users=new ArrayList<ContactsBean>();
			try {
				while(result.next()){
					
					
					ContactsBean  contacts=new ContactsBean();
					contacts.setCategoryId(Integer.parseInt(result.getString("CategoryId")));
					contacts.setAddress(result.getString("address"));
					contacts.setContactName(result.getString("ContactName"));
					contacts.setContectNum(result.getString("ContactNum"));
					contacts.setMark(result.getString("mark"));
					contacts.setContactId(Integer.parseInt(result.getString("ContactId")));
					users.add(contacts);
				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				WriteLog.getLog4j().logger.debug(e.getMessage());
			}
			return users;
		}
}
